﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EmployeeManagement
{
    public partial class EmpEditForm : Form
    {
        string empid;       //全局变量
        //public 类名（参数）{}
        public EmpEditForm()
        {
            InitializeComponent();
        }
        public EmpEditForm(string empid)
        {
            InitializeComponent();
            this.empid = empid;
        }

        private void EmpEditForm_Load(object sender, EventArgs e)
        {
            this.button1.Enabled = true;
            this.button2.Enabled = false;
            BindCom();
            if (this.Text=="修改员工")
            {
                this.button2.Enabled = true;
                this.button1.Enabled = false;
                //sql命令
                string sql = $"select * from Dept inner join Emp on Dept.DeptID=Emp.DeptID where EmpID={empid}";
                //调用dbHelper中的查询方法
                DataSet ds = dbHelper.GetData(sql);
                //将数据显示在前台对应的控件上
                this.txtName.Text = ds.Tables[0].Rows[0]["EmpName"].ToString();
                this.txtAge.Text = ds.Tables[0].Rows[0]["Age"].ToString();
                this.txtTel.Text = ds.Tables[0].Rows[0]["Tel"].ToString();
                if (ds.Tables[0].Rows[0]["Sex"].ToString() == "男")
                {
                    //男被选中
                    this.ranMan.Checked = true;
                }
                else
                {
                    this.radioButton2.Checked = true;
                }
                //下拉列表
                this.comboBox1.SelectedValue = ds.Tables[0].Rows[0]["DeptID"].ToString();
            }
            
        }
        private void BindCom()
        {
            //绑定下拉列表
            //sql命令
            string sql = "select * from Dept";
            //2、调用dbHelper中的查询方法
            DataSet ds = dbHelper.GetData(sql);
            //3、告诉下拉列表数据源是谁
            this.comboBox1.DataSource = ds.Tables[0];
            //设置下拉列表显示的列
            this.comboBox1.DisplayMember = "DeptName";
            //设置下拉列表隐藏的列
            this.comboBox1.ValueMember = "DeptID";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //从前台获取用户输入的内容
            string name = this.txtName.Text;
            string age = this.txtAge.Text;
            string tel = this.txtTel.Text;
            string sex = this.ranMan.Checked ? "男" : "女";
            string id = this.comboBox1.SelectedValue.ToString();
            //验证姓名不能为空
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("姓名不能为空！");
                return;     //到此为止
            }
            if (string.IsNullOrEmpty(age))
            {
                MessageBox.Show("年龄不能为空！");
                return;     //到此为止
            }
            if (string.IsNullOrEmpty(tel))
            {
                MessageBox.Show("电话不能为空！");
                return;     //到此为止
            }
            //sql语句
            string sql = $"insert into Emp values('{name}','{sex}',{age},{tel},{id})";
            try
            {
                //调用dbhelper中的方法
                bool boo = dbHelper.ExecuteNonQuery(sql);
                //判断
                if (boo)
                {
                    MessageBox.Show("添加成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //从前台获取用户输入的内容
            string name = this.txtName.Text;
            string age = this.txtAge.Text;
            string tel = this.txtTel.Text;
            string sex = this.ranMan.Checked ? "男" : "女";
            string id = this.comboBox1.SelectedValue.ToString();
            //sql命令
            string sql = $"update Emp set EmpName='{name}',Sex='{sex}',Age={age},Tel='{tel}',DeptID={id} where EmpID={empid}";
            try
            {
                //调用dbhelper中的方法
                bool boo = dbHelper.ExecuteNonQuery(sql);
                //判断
                if (boo)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }
    }
}
